<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\BaseController;
use app\middleware\api\Auth;
use app\validate\api\CheckId;
use app\model\Carts as CartsModel;
class Carts extends BaseController
{
    protected $middleware=[Auth::class];

    /**
     * 购物车列表
     * @param int $limit
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function index($limit=10)
    {

      $data= CartsModel::where('users_id',request()->userid)
          ->with(['product','spec'])
          ->paginate($limit,false);

      return $this->success($data);
    }

    /**
     * 添加购物车
     * @return \think\response\Json
     * @throws \app\lib\exception\BaseException
     */
    public function add()
    {
        (new \app\validate\api\Carts())->goCheck();
        //加一个判断 同一个商品并且是同一个用户  修改数量
        CartsModel::create( request()->param());
        return $this->success();
    }

    /**
     * 修改购物车
     * @return \think\response\Json
     * @throws \app\lib\exception\BaseException
     */
    public function edit()
    {
        (new CheckId())->goCheck();
        $params=request()->param();
        CartsModel::where('id',$params['id'])->update($params);
        return $this->success();
    }

    /**
     * 删除购物车
     * @return \think\response\Json
     * @throws \app\lib\exception\BaseException
     */
    public function delete()
    {
        (new CheckId())->goCheck();
        $params=request()->param();
        CartsModel::where('id',$params['id'])->delete();
        return $this->success();
    }
}
